package com.mrdeer.uaa.repository;

import com.mrdeer.uaa.api.entity.OAuthClientDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ：xie jianchu
 * @date ：Created in 2020/9/9 0009 13:33
 */
@Repository("oauthRepositoryJdbc")
public class OAuthRepositoryJDBC implements OAuthRepository {

    private static OAuthClientDetailsRowMapper oAuthClientDetailsRowMapper = new OAuthClientDetailsRowMapper();

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public OAuthClientDetails findOAuthClientDetails(String clientId) {
        final String sql = "select * from oauth_client_details where client_id=?";
        final List<OAuthClientDetails> list = this.jdbcTemplate.query(sql, new Object[]{clientId}, oAuthClientDetailsRowMapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<OAuthClientDetails> findAllOAuthClientDetails() {
        final String sql = "select * from oauth_client_details order by create_time desc";
        return this.jdbcTemplate.query(sql, oAuthClientDetailsRowMapper);
    }

    @Override
    public void updateOAuthClientDetailsArchive(String clientId, boolean archive) {
        final String sql = "update oauth_client_details set archived=? where client_id=?";
        this.jdbcTemplate.update(sql, archive, clientId);
    }

    @Override
    public void saveOAuthClientDetails(OAuthClientDetails clientDetails) {
        final String sql = "insert into oauth_client_details(client_id,resource_ids,client_secret,scope," +
                "authorized_grant_types,web_server_redirect_uri,authorities,access_token_validity,refresh_token_validity,addition_information,trusted,autoapprove) " +
                "values (?,?,?,?,?,?,?,?,?,?,?)";

        this.jdbcTemplate.update(sql, ps -> {
            ps.setString(1, clientDetails.clientId());
            ps.setString(2, clientDetails.resourceIds());

            ps.setString(3, clientDetails.clientSecret());
            ps.setString(4, clientDetails.scope());

            ps.setString(5, clientDetails.authorizedGrantedTypes());
            ps.setString(6, clientDetails.webServerRedirectUri());

            ps.setString(7, clientDetails.authorities());
            ps.setObject(8, clientDetails.accessTokenValidity());

            ps.setObject(9, clientDetails.refreshTokenValidity());
            ps.setString(10, clientDetails.additionalInformation());

            ps.setBoolean(11, clientDetails.trusted());
            ps.setString(12, clientDetails.autoApprove());
        });
    }
}
